package com.wm.blog_web.controller;


import com.wm.blog_common.bean.QueryPage;
import com.wm.blog_common.domain.TCommentDO;
import com.wm.blog_common.entity.TComment;
import com.wm.blog_common.result.Result;
import com.wm.blog_web.controller.base.WebBaseController;
import com.wm.blog_web.query.TBlogQuery;
import com.wm.blog_web.query.TCommentQuery;
import com.wm.blog_web.service.TCommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Optional;

/**
 * <p>
 * 评论表 前端控制器
 * </p>
 *
 * @author 半卷流年
 * @since 2020-03-24
 */
@Slf4j
@RestController
@RequestMapping("/web/comment")
public class TCommentController extends WebBaseController {

    @Autowired
    private TCommentService tCommentService;

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<TCommentDO> get(@PathVariable Long id) {
        return tCommentService.selectById(id);
    }

    /**
     * 根据ID删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Long id){
        return tCommentService.deleteById(id);
    }

    /**
     * 添加
     * @param tComment
     * @return
     */
    @PostMapping
    public Result insert(@RequestBody TComment tComment){
        return tCommentService.insert(tComment);
    }

    /**
     * 修改
     * @param tComment
     * @return
     */
    @PutMapping
    public Result update(@RequestBody TComment tComment){
        return tCommentService.update(tComment);
    }

    /**
     * 分页查询
     * @param current   查询页
     * @param size   每页显示条数
     * @param query 查询参数
     * @return
     */
    @GetMapping("/page")
    public Result page(Integer current, Integer size, TCommentQuery query){
        return tCommentService.page(new QueryPage(current,size),query);
    }


    /**
     * 博客点赞
     *
     * @return
     */
    @PutMapping("/likeBlog")
    public Result likeBlog(TBlogQuery query, HttpServletRequest request) {
        query.setUserId(super.currentUserId(request));
        return tCommentService.likeBlog(query);
    }


    /**
     * 发表评论
     * @param query
     * @param request
     * @return
     */
    @PostMapping("/publishComment")
    public Result publishComment(@RequestBody @Valid TCommentQuery query, HttpServletRequest request){
        query = Optional.ofNullable(query).orElse(new TCommentQuery());
        query.setUserId(super.currentUserId(request));
        return tCommentService.publishComment(query);
    }


    /**
     * 点赞量
     * @param query
     * @return
     */
    @GetMapping("/likeCount")
    public Result likeCount(TCommentQuery query){
        return tCommentService.likeCount(query);
    }




}
